package cc.rengu.redp.bizimpl.upmp.controller;


import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.domain.RegionInfo;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.domain.RedpResponse;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.bizimpl.upmp.domain.MerGrpInfo;
import cc.rengu.redp.bizimpl.upmp.service.MerGrpInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * @author LiuYuLiang.LL
 */
@Slf4j
@Validated
@Api(tags = "参数管理:商户组别管理")
@RestController
@RequestMapping("/admin/merParam/merGrpInfo")
public class MerGrpInfoController extends BaseController {
    @Autowired
    private MerGrpInfoService merGrpInfoService;

    @GetMapping
//    @RequiresPermissions("areaInfo:view")
    public Map<String, Object> list(QueryRequest request, MerGrpInfo entity) {
        return getDataTable(this.merGrpInfoService.findByPage(request, entity));
    }

    @GetMapping("/unionMerGrpSelect2List")
    public List<MerGrpInfo> unionMerGrpSelect2List(String q) {
        LambdaQueryWrapper<MerGrpInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(q)) {
            queryWrapper.or(qw -> qw.likeRight(MerGrpInfo::getGrpNo, q).likeRight(MerGrpInfo::getGrpDesc, q));
        }
        queryWrapper.eq(MerGrpInfo::getStatus,  BusinessContainer.data_sta_01);
        return this.merGrpInfoService.list(queryWrapper);
    }

    @Log("查询单条")
    @GetMapping(value = "/getOne")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public MerGrpInfo getOne(@NotBlank(message = "{required}") String id) {
        return merGrpInfoService.getById(id);
    }

    @Log("新增")
    @PostMapping
//    @RequiresPermissions("areaInfo:add")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "grpNo", value = "组别编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "grpDesc", value = "组别描述", required = true, dataType = "String")
    })
    public void add(@Valid MerGrpInfo entity) throws RedpException {
        try {
            this.merGrpInfoService.create(entity);
        } catch (Exception e) {
            String message ="";
            MerGrpInfo merGrpInfo = merGrpInfoService.findByGrpNo(entity.getGrpNo());
            if (merGrpInfo!=null){
                message = "组别编号重复";
            }else {
                message = "新增失败";
            }
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("修改")
    @PutMapping("/update")
//    @RequiresPermissions("areaInfo:update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    })
    public void update(@Valid MerGrpInfo entity) throws RedpException {
        try {
            this.merGrpInfoService.update(entity);
        } catch (Exception e) {
            String message = "修改失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("注销")
    @PutMapping("/cancel")
//    @RequiresPermissions("areaInfo:cancel")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public void cancel(@NotBlank(message = "{required}") String id) throws RedpException {
        try {
            this.merGrpInfoService.cancel(id);
        } catch (Exception e) {
            String message = "注销失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("删除")
    @DeleteMapping("/{ids}")
//    @RequiresPermissions("areaInfo:delete")
    @ApiImplicitParam(paramType = "path", name = "ids", value = "id主键，逗号隔开", required = true, dataType = "String")
    public void delete(@NotBlank(message = "{required}") @PathVariable String ids) throws RedpException {
        try {
            this.merGrpInfoService.delete(ids.split(StringPool.COMMA));
        } catch (Exception e) {
            String message = "删除失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @PostMapping(value = "/check")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "grpNo", value = "组别代码", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "grpDesc", value = "组别描述", dataType = "String")
    })
    public RedpResponse check(String id, String grpNo, String grpDesc) throws RedpException {
        LambdaQueryWrapper<MerGrpInfo> wrapper = new LambdaQueryWrapper();
        if (StringUtils.checkValNotNull(grpNo)) {
            if (StringUtils.checkValNotNull(id)) {
                wrapper.ne(MerGrpInfo::getId, id);
            }
            wrapper.eq(MerGrpInfo::getGrpNo, grpNo);
            MerGrpInfo info = merGrpInfoService.getOne(wrapper);
            if (null != info) {
                throw new RedpException("[ 组别代码 ] 重复，请重新输入");
            }
        }
       if (StringUtils.checkValNotNull(grpDesc)) {
            wrapper = new LambdaQueryWrapper();
            if (StringUtils.checkValNotNull(id)) {
                wrapper.ne(MerGrpInfo::getId, id);
            }
            wrapper.likeRight(MerGrpInfo::getGrpDesc, grpDesc);
            MerGrpInfo info = merGrpInfoService.getOne(wrapper);
            if (null != info) {
                throw new RedpException("[ 组别描述 ] 重复，请重新输入");
            }
        }
        return new RedpResponse().message("验证成功");
    }
}
